﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HypermarketSerialPort
{
    class CashierRegister
    {
        private static string mCashierID;
        private static string mMachineID;
        private string mTransactionID;
        private DateTime mTransactionDate;
        private Dictionary<string, Transaction> mTransaction;
        private bool mIsTransactionStarted;

        public CashierRegister(string cashierID, string machineID)
        {
            mCashierID = cashierID;
            mMachineID = machineID;
            mIsTransactionStarted = false;
        }

        public void StartTransaction(string transactionID)
        {
            mTransactionID = transactionID;
            mTransactionDate = DateTime.Now;
            mIsTransactionStarted = true;
            mTransaction = new Dictionary<string, Transaction>();
        }

        public void AddTransaction(string barcode, string name, string batchId, string costPrice, 
                                        string sellingPrice, string discount, int unitSold)
        {
            try
            {
                Transaction inTransaction = new Transaction();
                inTransaction.Barcode = barcode;
                inTransaction.ProductName = name;
                inTransaction.BatchID = batchId;
                inTransaction.CostPirce = costPrice;
                inTransaction.SellingPrice = sellingPrice;
                inTransaction.Discount = discount;
                inTransaction.UnitSold = unitSold;
                Transaction outTransaction;
                if(mTransaction.TryGetValue(barcode, out outTransaction))
                {
                    outTransaction.UnitSold = outTransaction.UnitSold + unitSold;
                    mTransaction[barcode] = outTransaction;
                }
                else
                {
                    mTransaction.Add(barcode, inTransaction);
                }
            }
            catch
            {
                throw;
            }
        }

        public bool CancelTransaction(string barcode)
        {
            try
            {
                bool isFound = mTransaction.Remove(barcode);
                //isFound = mTransaction.Remove(mTransaction.Keys.Last());
                return isFound;
            }
            catch
            {
                throw;
            }
        }

        public void CancelAllTransactions()
        {
            try
            {
                mIsTransactionStarted = false;
                mTransaction.Clear();
            }
            catch
            {
                throw;
            }
        }

        public List<Transaction> CheckOut()
        {
            try
            {
                List<Transaction> transactionList = new List<Transaction>();
                foreach (KeyValuePair<string, Transaction> val in mTransaction)
                {
                    Transaction tr = val.Value;
                    //string query = "INSERT INTO CheckOutItem VALUES('" + mTransactionID + "','" + tr.Barcode + "','" +
                    //    tr.BatchID + "','" + tr.CostPirce + "," + tr.SellingPrice + "," + tr.Discount + "," + tr.UnitSold + ");";
                    transactionList.Add(tr);
                }
                mTransaction.Clear();
                mIsTransactionStarted = false;
                return transactionList;
            }
            catch
            {
                throw;
            }
        }

        public bool isTransactionStarted() { return mIsTransactionStarted; }
        public string getMachineID() { return mMachineID; }
        public string getCashierID() { return mCashierID; }
        public string getTransactionID() { return mTransactionID; }
        public DateTime getTransactionDate() { return mTransactionDate; }
    }
}
